<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of registration
 *
 * @author tejomurti
 * 
 * Class Registrasi Member via REST
 */
class registration {
    //put your code here
    private $config=array();
    
    //property
    public $sponsor;
    public $upline;
    public $position;
    public $serial;
    public $pin;
    public $name;
    public $phone_no;
    public $city;
    
    /**
     * bangung class <br />
     * Deklarasikan variable
     * 
     */
    public function __construct() {
    
        set_time_limit(0);
        ini_set('memory_limit', '1500M');
        Yii::import('application.mlm.core.mlm_function');
        Yii::import('application.mlm.core.binary.bonus.bonus_gen_node_products');
        Yii::import('application.mlm.core.mlm_lib_gi');
        Yii::import('application.mlm.core.binary.*');
        Yii::import('application.mlm.core.matrix.*');
        Yii::import('application.mlm.core.board.*');
    }
    
    public function build_data()
    {
        $this->config['registrasi_form']=array(
            'member_sponsor'=>$this->sponsor,
            'member_upline'=>$this->upline,
            'position'=>$this->position,
            'serial_id'=>$this->serial,
            'serial_pin'=>$this->pin,
            'member_name'=>$this->name,
            'member_mobilephone'=>$this->phone_no,
            'member_address'=>$this->city,
        );
        
        return $this->config;
    }
    
    /**
     * validasi variable
     */
    public function validation($build_data=array())
    {
        
        $model=new registrasi_form('registrasi');
        $arr_card=array();
        $arr_member=array();
        $arr_network=array();
        
        //$build_data=$this->build_data();
        $result=array(
            'status'=>false,
            'message'=>'Belum ada input Data',
        );
        if(!empty($build_data))
        {
            $model->setAttributes($build_data['registrasi_form'], false);
            $model->member_nickname=  str_replace(' ', '_', strtolower($model->member_name));
            $model->fix='active';
            
            if(strtoupper($model->position)!='L' AND strtoupper($model->position)!='R')
            {
                $result=array(
                'status'=>false,
                'message'=>'Posisi anda masih salah, yang diizinkan hanya L atau R ',
                ); 
            }
            elseif($model->validate())
            {
                
                 //ambil data yang ada di attribut
                //network nya
                $password= substr(uniqid(),0,5);
                $arr_network['sponsor_mid'] = $model->member_sponsor;
                $arr_network['upline_mid'] = $model->member_upline;
                $arr_network['position'] = $model->position;
                //serial nya
                $arr_card['serial_id'] = $model->serial_id;
                $arr_card['serial_mid'] = '';
                $arr_card['serial_pin'] = $model->serial_pin;
                //data membernya
                //$arr_member['register_id'] = $model['register_id'];
                $arr_member['password'] = md5($password);
                $arr_member['member_nickname'] = $model->member_nickname;
                $arr_member['member_name'] = $model->member_name;
                $arr_member['member_email'] = '-';
                $arr_member['member_phone'] = '-';
                $arr_member['member_mobilephone'] = $model->member_mobilephone;

                
                /*
                 * BANK 
                 */
                $arr_member['member_bank_account_name'] = $model->member_name;
                $arr_member['member_bank_id'] = '';
                $arr_member['member_bank_city'] = '';
                $arr_member['member_bank_branch'] = '';
                $arr_member['member_bank_account_no'] = '';
                
//                echo '<pre>';
//                print_r($arr_member);
//                echo '</pre>';
               // exit;
                //saatnya registrasi
                $mlm = new mlm_lib_gi();
                //data array buat bonus distributor
                $bonus_arr = array('sponsor', 'sponsor_upgrade', 'match', 'gen_node', 'gen_node_extra', 'gen_node_products', 'gen_match','extra_sponsor');
                $mlm->set_bonus($bonus_arr)
                     ->set_date(date("Y-m-d"))
                     ->set_hu(7);
                $mlm->set_data_serial($arr_card)
                        ->set_data_member($arr_member)
                        ->set_data_network($arr_network);
                $member_network_id = $mlm->get_network_id_arr();
                $network_mid = function_lib::get_network_mid($member_network_id[1]);
                $network_id = $member_network_id[1];
                $sponsor = new mlm_sponsor;
                $sponsor->set_date(date("Y-m-d"))->gen_node(function_lib::get_network_id($model->member_sponsor));



                //sekarang buat bonus pasangan
                $match = new mlm_match;
                $match->set_date(date("Y-m-d"));
                $match->execute();
                //buat jaringan matrixnya
                $mlm_matrix = new mlm_matrix;
                $mlm_matrix->set_date(date("Y-m-d"))->set_height(10)->set_width(4)->set_network_id($network_id)->set_network_mid($network_mid)->insert_node();
//                
                //if(trim($mlm->get_message())!='')
                //{
                    $message=$mlm->get_message();
               // }
                //else
               // {
                     $message='Selamat, proses registrasi berhasil. <br /> Anda dapat login menggunakan: 
                    <br />Id Member: '.$network_mid.
                    '<br />Password: '.$password;
              //  }
               
                $result=array(
                    'status'=>true,
                    'message'=>$message,
                );
            }
            else
            {
                $result=array(
                'status'=>false,
                'message'=>CHtml::errorSummary($model),
                );
            }
            
            
        }
        
        return $result;
    }
    
}

?>
